﻿using System;
using System.Collections.Generic;

namespace BLL.Core
{
    //
    // 摘要:
    //     Use this collection to store mapping from the Type of a service to the object
    //     that provides it in a way that is aware of embedded types. The comparer for this
    //     collection will perform a comparison of the Guid properties of the Type key instead
    //     of doing a reference comparison which will fail in type embedding scenarios.
    //     This collection is not thread-safe.
    public class ServiceCollection<T> : Dictionary<Type, T>
    {
        private class EmbeddedTypeAwareTypeComparer : IEqualityComparer<Type>
        {
            public bool Equals(Type x, Type y)
            {
                return x.GUID == y.GUID;
            }

            public int GetHashCode(Type obj)
            {
                return obj.GUID.GetHashCode();
            }
        }

        private static EmbeddedTypeAwareTypeComparer serviceTypeComparer = new EmbeddedTypeAwareTypeComparer();

        public ServiceCollection()
            : base((IEqualityComparer<Type>)serviceTypeComparer)
        {
        }
    }

}
